import axios from "axios";

const list = {
  namespaced: true,
  state: {
    //所有任务的列表
    list: [],
    //文本框的内容
    inputValue: "aaa",
    //下一个id
    nextId: 5,
    viewKey: "all",
  },
  getters: {
    unDoneLength(state) {
      return state.list.filter((x) => x.done === false).length;
    },
    infolist(state) {
      if (state.viewKey === "all") {
        return state.list;
      }
      if (state.viewKey === "undone") {
        return state.list.filter((x) => !x.done);
      }
      if (state.viewKey === "done") {
        return state.list.filter((x) => x.done);
      }
      return state.list;
    },
  },
  mutations: {
    initList(state, list) {
      state.list = list;
    },
    changeViewKey(state, key) {
      state.viewKey = key;
    },
  },
  actions: {
    getList(context) {
      axios.get("/list.json").then((data) => {
        console.log(data);
        context.commit("initList", data.data);
      });
    },
  },
};

export default list;
